package com.krain.ddbb.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.krain.corelibrary.widget.recyleview.BaseRecyclerViewAdapter;
import com.krain.ddbb.R;
import com.krain.ddbb.activity.UserOrderActivity;
import com.krain.ddbb.base.APP;
import com.krain.ddbb.base.APP_;
import com.krain.ddbb.base.BaseActivity;
import com.krain.ddbb.entity.AppApi;
import com.krain.ddbb.entity.BaseEntity;
import com.krain.ddbb.entity.RobOrderBean;
import com.krain.ddbb.util.BaseUtil;

import org.androidannotations.annotations.App;

import java.util.List;

/**
 * Created by Nick.Hu on 2016/10/17.
 * Mail:huyamin@yonghui.cn | huyamin@hotmail.com
 * Tel: +8618521550285
 */

public class RobOrderListAdapter extends BaseRecyclerViewAdapter<RobOrderListAdapter.Viewholder> {
    List<RobOrderBean> mList;
    Activity context;
    APP app;
    BaseUtil baseUtil;

    public RobOrderListAdapter(List<RobOrderBean> mList, Activity context, APP app, BaseUtil baseUtil) {
        this.mList = mList;
        this.context = context;
        this.app = app;
        this.baseUtil = baseUtil;
    }

    @Override
    public RobOrderListAdapter.Viewholder getViewHolder(View itemView) {
        return new Viewholder(itemView);
    }

    @Override
    public View getItemView(int viewType, ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_roborder_list,null);
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        return view;
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public void onBindViewHolder(Viewholder viewholder, int position) {
        final RobOrderBean robOrderBean = mList.get(position);
        viewholder.num.setText(!TextUtils.isEmpty(robOrderBean.getOrder_num())?robOrderBean.getOrder_num():"");
        viewholder.desc.setText(!TextUtils.isEmpty(robOrderBean.getDesc())?robOrderBean.getDesc():"");
        viewholder.price.setText(!TextUtils.isEmpty(robOrderBean.getServer_price())?robOrderBean.getServer_price():"");
        viewholder.time.setText(!TextUtils.isEmpty(robOrderBean.getPublish_time())?robOrderBean.getPublish_time():"");
        viewholder.submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        APP_ app = (APP_) context.getApplicationContext();
                        BaseEntity entity = AppApi.getsInstance(context).robOrdeer(app.getmUserinfo().getAccess_token(),robOrderBean.getId());
                        if (entity != null) {
                            context.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    baseUtil.showSnackBar(context,"恭喜抢单成功","点击去查看详情",toDetailListener);
                                }
                            });

                        }else {
                            context.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    baseUtil.showSnackBar(context,"抢单失败,您的单子被别人抢走啦！","关闭页面",onClickListener);
                                }
                            });

                        }
                    }
                }).start();

            }
        });
    }


    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            context.finish();
        }
    };

    View.OnClickListener toDetailListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            UserOrderActivity.jumpTothisActivity((BaseActivity) context);
        }
    };

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class Viewholder extends RecyclerView.ViewHolder{
        public TextView num,desc,price,time,submit;

        public Viewholder(View itemView) {
            super(itemView);
            num = (TextView) itemView.findViewById(R.id.rob_list_num);
            desc = (TextView) itemView.findViewById(R.id.rob_list_desc);
            price = (TextView) itemView.findViewById(R.id.rob_list_price);
            time = (TextView) itemView.findViewById(R.id.rob_list_time);
            submit = (TextView) itemView.findViewById(R.id.rob_list_submit);
        }
    }
}
